package org.liveboardgames.agricola.control.delegates
{
    import com.adobe.cairngorm.business.ServiceLocator;
    
    import flash.utils.setTimeout;
    
    import mx.rpc.IResponder;
    
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;
    import org.liveboardgames.agricola.vo.farm.FarmTile;
    import org.liveboardgames.agricola.vo.player.Player;
    
    public class SowDelegate
    {
        private var _responder:IResponder;
        
        private var _service:Object;
        
        public function SowDelegate(responder:IResponder)
        {
            this._service = ServiceLocator.getInstance().getRemoteObject( "farmService" );
            this._responder = responder;
        }
        
        public function sow(player:Player, resource:String, tile:FarmTile):void
        {
        	// Even if the player id is not used yet, it will be needed later on when cards will be implemented 
        	var call:Object = _service.sow(AgricolaModelLocator.getInstance().game.id, player.farm.id, resource, tile.uniqueId);
			call.resultHandler = _responder.result;
			call.faultHandler = _responder.fault;
//            setTimeout(sowService, 0, player, resource, tile);
        }
        
        private function sowService(player:Player, resource:String, tile:FarmTile):void
        {
            if (ResourceTypeEnum.GRAIN == resource)
            {
                tile.addResource(resource, 3);
            }
            else if (ResourceTypeEnum.VEGETABLE == resource)
            {
                tile.addResource(resource, 2);
            }
        }
    }
}